#!/usr/bin/perl

require './web-lib.pl';
@available = ("webmin", "system", "servers", "cluster", "hardware", "", "net","controller");
&init_config();

$hostname = &get_system_hostname();
$ver = &get_webmin_version();
&get_miniserv_config(\%miniserv);
if ($gconfig{'real_os_type'}) {
	if ($gconfig{'os_version'} eq "*") {
		$ostr = $gconfig{'real_os_type'};
		}
	else {
		$ostr = "$gconfig{'real_os_type'} $gconfig{'real_os_version'}";
		}
	}
else {
	$ostr = "$gconfig{'os_type'} $gconfig{'os_version'}";
	}
&ReadParse();

#added by setup wizard
if ($in{'cat'} eq "")
{
	($curstep, $stepsref) = &get_wizard_conf();
	if(($curstep eq "") or ($curstep <=@$stepsref))
	{
		&redirect("setupwiz.cgi");
	}
}
#-----------------------

# Redirect if the user has only one module
&read_acl(undef, \%uacl);
local @uacl = @{$uacl{$base_remote_user}};
if (@uacl == 1 && $gconfig{'gotoone'}) {
	&redirect("$uacl[0]/");
	exit;
	}

# Show standard header
$gconfig{'sysinfo'} = 0;
$theme_index_page = 1;
&header($gconfig{'mainbrand'}, "",
	undef, undef, 1, 1);

if (!@ezstor_modules) {
	# use has no modules!
	my $msg_mainnone = &text('main_none', $gconfig{'mainbrand'});
	print "<p><b>$msg_mainnone</b><p>\n";
	}
elsif ($gconfig{"notabs_${base_remote_user}"} == 2 ||
    $gconfig{"notabs_${base_remote_user}"} == 0 && $gconfig{'notabs'}) {
	# Generate main menu with all modules on one page
#	print "<center><table border=1 cellpadding=0>\n";
#	$pos = 0;
#	$cols = $gconfig{'nocols'} ? $gconfig{'nocols'} : 4;
#	$per = 100.0 / $cols;
#	foreach $m (@ezstor_modules) {
#		if ($pos % $cols == 0) { print "<tr>\n"; }
#		print "<td valign=top align=center>\n";
#		print "<table border><tr><td><a href=/$m->{'dir'}/>",
#		      "<img src=$m->{'dir'}/images/icon.gif border=0 ",
#		      "width=48 height=48></a></td></tr></table>\n";
#		print "<a href=/$m->{'dir'}/>$m->{'desc'}</a></td>\n";
#		if ($pos % $cols == $cols - 1) { print "</tr>\n"; }
#		$pos++;
#		}
#	print "</table></center><p><table width='100%' bgcolor='#FFFFFF'><tr><td></td></tr></table><br>\n";
	}
else {
	# Generate categorized module list
#	print "<table border=1 cellpadding=0 cellspacing=0 width=95% align=center><tr><td><table border=0 cellpadding=0 cellspacing=0 height=20><tr>\n";
	print qq~<table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
    <td  height="45">&nbsp;</td>

    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="230" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="50" height="87">&nbsp;</td>
          <td valign="bottom"><img src="/images/home_001.jpg" height="70" width="230"></td>
        </tr>
        <tr>
          <td height="335" colspan="2" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="15"></td>
              </tr>
              <tr>
                <td height="6" bgcolor="#FF0000"></td>
              </tr>
            </table></td>
        </tr>
      </table></td>
    <td valign="top"><table width="100%" height="230" border="0" cellpadding="2" cellspacing="0">~;

	$usercol = defined($gconfig{'cs_header'}) ||
		   defined($gconfig{'cs_table'}) ||
		   defined($gconfig{'cs_page'});
#	foreach $c (@cats) {
#		$t = $cats{$c};
#		if ($in{'cat'} eq $c) {
#			print "<td bgcolor=#bae3ff>",
#			  "<img src=images/tabs/blue_left.jpg alt=\"\">","</td>\n";
#			print "<td bgcolor=#bae3ff>&nbsp;<b>$t</b>&nbsp;</td>\n";
#			print "<td bgcolor=#bae3ff>",
#			  "<img src=images/tabs/blue_right.jpg alt=\"\">","</td>\n";
#			}
#		print "<td width=10></td>\n";
#		}
#	print "</tr></table> <table border=1 cellpadding=0 cellspacing=0 ",
#              "width=100% bgcolor=#FFFFFF background=images/msctile2.jpg>\n";
#	print "<tr><td><table width=100% cellpadding=5>\n";

	# Display the modules in this category
	$pos = 0;
	$cols = $gconfig{'nocols'} ? $gconfig{'nocols'} : 4;
	$per = 100.0 / $cols;
	$mods_ct = 0;
	foreach $m (@ezstor_modules) {
		$mods_ct++ if ($m->{'category'} eq $in{'cat'});
	}

	foreach $m (@ezstor_modules) {
		next if ($m->{'category'} ne $in{'cat'});

#		if ($pos % $cols == 0) { print "<tr>\n"; }
#		print "<td valign=top align=center width=$per\%>\n";
#		print "<table border bgcolor=#ffffff><tr><td><a href=/$m->{'dir'}/>",
#		      "<img src=$m->{'dir'}/images/icon.gif alt=\"\" border=0></a>",
#		      "</td></tr></table>\n";
#		print "<a href=/$m->{'dir'}/><font color=#000000>$m->{'desc'}</font></a></td>\n";
#		if ($pos++ % $cols == $cols - 1) { print "</tr>\n"; }
		if ($m->{'desc'} =~ /^(.*)\|(.*)$/)
		{
			$m_sdesc = $1;
			$m_ldesc = $2;
		}
		else
		{
			$m_sdesc = $m->{'desc'};
			$m_ldesc = "";
		}

		if ($mods_ct > 4)
		{
			if ($pos % 2 == 0)
			{
				print qq~<tr><td width="50%" valign="top"><table width="100%" cellpadding="2"><tr>
          			<td width="8%" height="34">&nbsp;</td>
          			<td width="4%" valign="bottom"><a href="/$m->{'dir'}/" target="_top"><img src="/images/home_002.gif"  border="0"></a></td>
          			<td width="2%">&nbsp;</td>
          			<td width="82%" valign="bottom"><a href="/$m->{'dir'}/" target="_top" class="p16">$m_sdesc</a></td>
        			</tr>~;
				print qq~<tr>
          			<td width="8%" height="16">&nbsp;</td>
          			<td width="4%">&nbsp;</td>
          			<td width="2%">&nbsp;</td>
          			<td width="82%" class="p13" valign="top">$m_ldesc</td>
        			</tr></table></td>~;
			}
			else
			{
				print qq~<td width="50%" valign="top"><table width="100%" cellpadding="2"><tr>
          			<td width="4%" height="34">&nbsp;</td>
          			<td width="2%" valign="bottom"><a href="/$m->{'dir'}/" target="_top"><img src="/images/home_002.gif"  border="0"></a></td>
          			<td width="1%">&nbsp;</td>
          			<td width="41%" valign="bottom"><a href="/$m->{'dir'}/" target="_top" class="p16">$m_sdesc</a></td>
        			</tr>~;
				print qq~
          			<td width="4%" height="16">&nbsp;</td>
          			<td width="2%">&nbsp;</td>
          			<td width="1%">&nbsp;</td>
          			<td width="41%" class="p13" valign="top">$m_ldesc</td>
        			</tr></table></td></tr>~;
			}
		}
		else
		{
			print qq~<tr>
          		<td width="8%" height="34">&nbsp;</td>
          		<td width="4%" valign="bottom"><a href="/$m->{'dir'}/" target="_top"><img src="/images/home_002.gif"  border="0"></a></td>
          		<td width="2%">&nbsp;</td>
          		<td width="82%" valign="bottom"><a href="/$m->{'dir'}/" target="_top" class="p16">$m_sdesc</a></td>
        		~;
			print qq~<tr>
          		<td width="8%" height="16">&nbsp;</td>
          		<td width="4%">&nbsp;</td>
          		<td width="2%">&nbsp;</td>
          		<td width="82%" class="p13" valign="top">$m_ldesc</td>
        		</tr>~;
		}
		$pos ++;
		}
#	while($pos++ % $cols) {
#		print "<td width=$per\%></td>\n";
#		}
#	print "</table></td></tr></table></td></tr></table>";

#    print qq~<table width="95%" border="1" cellspacing="0" cellpadding="0" align="center">
#  <tr>
#    <td background="images/white_bar.jpg" nowrap><img src="images/white_bar.jpg"></td>
#  </tr>
#</table>~;

#    print qq~<p><table width="98%" border="1" cellspacing="0" cellpadding="0" height="4" align="center">
#  <tr>
#    <td background="images/white_bar2.jpg" nowrap><img src="images/white_bar2.jpg"></td>
#  </tr>
#</table><p>~;
	print qq~<tr>
          	<td colspan="4">&nbsp;</td>

        	</tr>~;
	print qq~</table>
      	<br>~;
      	#<img align="right" src="/images/home_003.jpg" width="400" height="300"> </td>
  	print qq~</td></tr>
	</table>~;


	}

#if ($miniserv{'logout'} && !$gconfig{'alt_startpage'} &&
#    !$ENV{'SSL_USER'} && !$ENV{'LOCAL_USER'} &&
#    $ENV{'HTTP_USER_AGENT'} !~ /webmin/i) {
#	print "<table border=1 width=100% cellpadding=0 cellspacing=0><tr>\n";
#	if ($gconfig{'skill_'.$base_remote_user}) {
#		print "<td><b>$text{'main_skill'}:</b>\n";
#		foreach $s ('high', 'medium', 'low') {
#			print "&nbsp;|&nbsp;" if ($done_first_skill++);
#			if ($gconfig{'skill_'.$base_remote_user} eq $s) {
#				print $text{'skill_'.$s};
#				}
#			else {
#				print "<a href='switch_skill.cgi?skill=$s&",
#				   "cat=$in{'cat'}'>", "<font color=000000>", $text{'skill_'.$s},"</font></a>";
#				}
#			}
#		print "</td>\n";
#		}
#   print "<table border=1 width=95% align=center><tr><td width=100%><b><font color='#FFFFFF'>&nbsp;&nbsp;";
#    print &text('main_version', $ver, $hostname, $ostr)
#	if (!$gconfig{'nohostname'});
#    print "</font></b>\n";
#    print "</td>\n";

#print "<td align=right><a href='http://www.msclinux.com/'>",
#          "<img src='images/theme_by.jpg' border='0'></a>&nbsp;&nbsp;</div><br>\n";
#    print "</tr></table>\n";

#	}

&footer();


sub chop_font {

        foreach $l (split(//, $t)) {
            $ll = ord($l);
            if ($ll > 127 && $lang->{'charset'}) {
                print "<img src=images/letters2/$ll.$lang->{'charset'}.gif alt=\"$l\" align=bottom border=0>";
                }
            elsif ($l eq " ") {
                print "<img src=images/letters2/$ll.gif alt=\"\&nbsp;\" align=bottom border=0>";
                }
            else {
                print "<img src=images/letters2/$ll.gif alt=\"$l\" align=bottom border=0>";
                }
            }

}
